package com.hy.convenient.controller.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hy.convenient.common.LayuiToTable;
import com.hy.convenient.common.ResponseMessage;
import com.hy.convenient.pojo.login.GoodItemPojo;
import com.hy.convenient.pojo.login.UsersPojo;
import com.hy.convenient.service.login.GoodItemMapperService;
import com.hy.convenient.service.login.SystenMenuService;
import com.hy.convenient.tool.CommonSeesionTool;
import com.hy.convenient.tool.PagingTool;
import com.hy.convenient.tool.RedisUitl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SystenMenuController {
    @Autowired
    private RedisUitl redisUitl;
    @Autowired
    private SystenMenuService systenMenuService;
    @Autowired
    private GoodItemMapperService goodItemMapperService;

    @RequiresPermissions({"user:menu:get"})
    @RequestMapping("/getMenu")
    public Map<String, Object> getMenu() {
        return ResponseMessage.successful(systenMenuService.getAll());
    }

    @RequiresPermissions({"user:admin"})
    @RequestMapping("/getGoodItem")
    public Map<String, Object> getGoodItem(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int limit, HttpServletRequest httpRequest) {
        UsersPojo usersPojo = redisUitl.getObject(CommonSeesionTool.getSeesionId(httpRequest));
        Map<String, Object> map = new HashMap<>();
        map.put("merid", usersPojo.getMerId());
        map.put("csbm", "");
        map.put("page", PagingTool.statrSize(page,limit));
        map.put("szie", limit);
        List<GoodItemPojo> list = goodItemMapperService.queryAll(map);
        int count = goodItemMapperService.queryConut(map);
        JSONArray array = JSONArray.parseArray(JSON.toJSONString(list));
        return LayuiToTable.layuiToTable(array, count);
    }
}
